package cn.it.hbtt.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.it.hbtt.daoimg.Buycarimg;
import cn.it.hbtt.daoimg.Orderimg1;
import cn.it.hbtt.daoimg.Usering;
import cn.it.hbtt.entity.User;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String pwd = request.getParameter("password");
			Usering use=new Usering();
			use.use();
			User u = null;
			String name1 = null, pwd1 = null;
			for (int i = 0; i < use.use().size(); i++) {
				u = (User) use.use().get(i);
				name1 = u.getLoginName();
				pwd1 = u.getPassword();
				if (name.equals(name1) && pwd.equals(pwd1)) {
					break;
				}
			}

			
			//Buycarimg buy=new Buycarimg();
			Orderimg1 ord=new Orderimg1();
			if (name.equals(name1) && pwd.equals(pwd1)) {
				HttpSession s = request.getSession();
				s.setAttribute("q", u);
				s.setAttribute("q2", ord.ord(name));
				//s.setAttribute("aa", buy.buycar(name));
				//s.setAttribute("q3", cost);
				String str = "<script  type='text/javascript'>";
				// str+="alert('登录成功！');";
				str += "location.href='http://localhost:8080/easybuy/test'";
				str += "</script>";
				out.write(str);
				out.flush();

			} else {
				String str = "<script  type='text/javascript'>";
				str += "alert('该账号没有注册或密码错误！');";
				str += "location.href='http://localhost:8080/easybuy/Login.jsp'";
				str += "</script>";
				out.write(str);
				out.flush();

			}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}